import json
import json
import re

from apps.crawler_platform.core_platform.core_g import CoreSqlValue
from apps.crawler_platform.core_platform.g_model import DealModel, CallBackModel, \
    DealInsertModel, PaperDaysModel, PaperArticleModel

__all__ = [
            "beijingribao_beijingribaostart_callback",
            "beijingribao_beijingribaodays_callback",
            "beijingribao_beijingribaoarticle_callback",
           ]


def beijingribao_beijingribaostart_callback(callmodel: CallBackModel[PaperDaysModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts
    data = para_dicts["data"]
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "task_tag_next": task_info.task_tag_next}
    if "1_1" in data:
        d_i_model = DealInsertModel()
        d_i_model.insert_pre = CoreSqlValue.insert_ig_it
        html = data["1_1"]['html'].replace('\r', '').replace('\n', '')
        date_list = re.findall('\[.*?\]', html)[0]
        for date in eval(date_list):
            temp = info_dicts.copy()
            temp["task_tag"] = temp["task_tag_next"]
            del temp["task_tag_next"]
            year = date[:4]
            months = date[4:6]
            days = date[-2:]

            temp["year"] = year
            temp["months"] = months
            temp["days"] = days
            temp["url"] = ''
            temp["days_json"] = '{}'
            d_i_model.lists.append(temp)
        result.befor_dicts.insert.append(d_i_model)
    return result


def beijingribao_beijingribaodays_callback(callmodel: CallBackModel[PaperDaysModel]) -> DealModel:
    result = DealModel()
    para_dicts = callmodel.para_dicts
    data = para_dicts["data"]
    task_info = callmodel.redis_all.parse_dict["1_1"].task_info
    info_dicts = {"task_name": callmodel.sql_model.task_name,
                  "task_tag": callmodel.sql_model.task_tag,
                  "task_tag_next": task_info.task_tag_next}
    if "1_1" in data:
        di_model = DealInsertModel()
        di_model.insert_pre = CoreSqlValue.insert_ig_it
        for item in data["1_1"]['info']['children']:
            col_title = item['col_title']
            year = callmodel.sql_model.year
            months = callmodel.sql_model.months
            days = callmodel.sql_model.days
            page = re.findall('第(.*?)版', col_title)[0]
            for art_item in item['children']:
                title = art_item['title']
                url_part = art_item['url_part']
                rawid_page = year + months + days + page.rjust(2, '0')
                index = re.findall(f'{page.rjust(3, "0")}_(.*?)\.', url_part)[0]

                temp_info = info_dicts.copy()
                task_tag_next = temp_info["task_tag_next"]
                temp_info["task_tag"] = task_tag_next
                del temp_info["task_tag_next"]
                temp_info["rawid"] = rawid_page + index.rjust(2, "0")
                new_dict = dict()
                new_dict["cid"] = rawid_page + index.rjust(2, "0")
                new_dict["year"] = year
                new_dict["months"] = months
                new_dict["days"] = days
                new_dict["versions_title"] = f'({page.rjust(2, "0")})：{col_title.split(" ", 1)[-1]}'
                new_dict["title"] = title
                new_dict["page"] = page.rjust(2, "0")
                new_dict["index"] = index
                new_dict["url"] = re.findall(f"{year}.*", url_part)[0]
                new_dict["versions_url"] = ''

                temp_info["article_json"] = json.dumps(new_dict, ensure_ascii=False)
                di_model.lists.append(temp_info)

        result.next_dicts.insert.append(di_model)

    return result


def beijingribao_beijingribaoarticle_callback(callmodel: CallBackModel[PaperArticleModel]) -> DealModel:
    result = DealModel()
    return result